package com.jungle.mvvm.sample.ui.main;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.app.SkinAppCompatDelegateImpl;

import com.gyf.immersionbar.ImmersionBar;
import com.jungle.mvvm.base.BaseActivity;
import com.jungle.mvvm.base.BaseViewModel;
import com.jungle.mvvm.sample.BR;
import com.jungle.mvvm.sample.R;
import com.jungle.mvvm.sample.databinding.ActivityMainBinding;
import com.jungle.mvvm.sample.ui.chat.ChatFragment;
import com.jungle.mvvm.sample.ui.home.HomeFragment;
import com.jungle.mvvm.sample.ui.mine.MineFragment;
import com.jungle.mvvm.sample.ui.project.ProjectFragment;

import java.util.ArrayList;
import java.util.List;

import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.listener.OnTabItemSelectedListener;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/3/5 17:58
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, BaseViewModel> {
    private List<Fragment> mFragments;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initParam() {
    }

    @Override
    public void initData() {
        // 初始化Fragment
        initFragment();
        // 初始化BottomTab
        initBottomTab();
    }

    private void initFragment() {
        mFragments = new ArrayList<>();
        mFragments.add(HomeFragment.newInstance());
        mFragments.add(ChatFragment.newInstance());
        mFragments.add(ProjectFragment.newInstance());
        mFragments.add(MineFragment.newInstance());

        commitAllowingStateLoss(0);
    }

    private void initBottomTab() {
        NavigationController navigationController = binding.pagerBottomTab.material()
                .addItem(R.mipmap.ic_main_home, "首页")
                .addItem(R.mipmap.ic_main_chat, "公众号")
                .addItem(R.mipmap.ic_main_project, "项目")
                .addItem(R.mipmap.ic_main_mine, "我的")
                .setDefaultColor(ContextCompat.getColor(this, R.color.colorAccent))
                .build();

        // Tab按钮切换事件
        navigationController.addTabItemSelectedListener(new OnTabItemSelectedListener() {
            @Override
            public void onSelected(int index, int old) {
                commitAllowingStateLoss(index);
            }

            @Override
            public void onRepeat(int index) {

            }
        });
    }

    private void commitAllowingStateLoss(int index) {
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(index + "");
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            currentFragment = mFragments.get(index);
            transaction.add(R.id.frameLayout, currentFragment, index + "");
        }
        transaction.commitAllowingStateLoss();
    }

    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int index = 0; index < mFragments.size(); index++) {
            Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(index + "");
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }

    @NonNull
    @Override
    public AppCompatDelegate getDelegate() {
        return SkinAppCompatDelegateImpl.get(this, this);
    }
}
